package kr.hkit.powerjava.chapter16.lab;

import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Date;
import java.util.Calendar;
import java.util.Random;

import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

public class CounterGame extends JFrame implements ActionListener {

	private JPanel contentPane;
	private JPanel panelText;
	private JPanel panelButton;
	private JLabel[] lbl;
	private int nums[];
	private JButton btnSpin;
	private JButton btnDate;
	private JButton btnImage;
	private ImageIcon icon[];

	public String getLbl(int i, String text) {
		return lbl[i].getText();
	}

	public void setLbl(int i, String text) {
		this.lbl[i].setText(text);
	}

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					CounterGame frame = new CounterGame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public CounterGame() {
		initialize();
	}

	private void initialize() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));

		panelText = new JPanel();
		contentPane.add(panelText);

		lbl = new JLabel[4];
		nums = new int[3];

		for (int i = 0; i < 3; i++) {
			lbl[i] = new JLabel(" 7 ");
			lbl[i].setFont(new Font("Serif", Font.BOLD | Font.ITALIC, 100));
			lbl[i].setSize(100, 100);
			lbl[i].setLocation(100 + 100 * i, 20);
			panelText.add(lbl[i]);
		}
		lbl[3] = new JLabel();
		lbl[3].setLocation(200, 40);
		panelText.add(lbl[3]);
		panelText.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

		icon = new ImageIcon[4];
		icon[0] = new ImageIcon("C:\\Users\\hkit\\Pictures\\1.jpg");
		icon[1] = new ImageIcon("C:\\Users\\hkit\\Pictures\\2.jpg");
		icon[2] = new ImageIcon("C:\\Users\\hkit\\Pictures\\3.jpg");
		icon[3] = new ImageIcon("C:\\Users\\hkit\\Pictures\\4.jpg");

		panelButton = new JPanel();
		contentPane.add(panelButton);

		btnImage = new JButton("이미지 스핀");
		btnImage.addActionListener(this);

		btnSpin = new JButton("스핀");
		panelButton.add(btnSpin);
		btnSpin.addActionListener(this);
		panelButton.add(btnImage);

		btnDate = new JButton("시간");
		btnDate.addActionListener(this);
		panelButton.add(btnDate);
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btnDate) {
			actionPerformedBtnDate(e);
		}
		if (e.getSource() == btnImage) {
			actionPerformedBtnImage(e);
		}
		if (e.getSource() == btnSpin) {
			actionPerformedBtnSpin(e);
		}
	}

	protected void actionPerformedBtnSpin(ActionEvent e) {
		int count = 0;

		for (int i = 0; i < 3; i++) {
			nums[i] = (int) (Math.random() * 10);
			lbl[i].setText(" " + nums[i] + " ");
			lbl[i].setIcon(null);
			for (int j = 0; j < i; j++) {
				if (i != j && nums[i] == nums[j]) {
					count++;
				}
			}
		}

		switch (count) {
			case 0:
				lbl[3].setText("일치하는 숫자 없음");
				break;
			case 1:
				lbl[3].setText("2개의 숫자만 일치");
				break;
			case 3:
				lbl[3].setText("3개의 숫자 모두 일치!!!");
				break;
		}

	}

	protected void actionPerformedBtnImage(ActionEvent e) {
		for (int i = 0; i < 3; i++) {
			lbl[i].setIcon(icon[(int) (Math.random() * 10 % 4)]);
			lbl[i].setText(null);
		}
		lbl[3].setText(null);
	}
	protected void actionPerformedBtnDate(ActionEvent e) {
		int count = 0;
		Random rnd = new Random();
		rnd.setSeed(System.currentTimeMillis());
		
		for (int i = 0; i < 3; i++) {
			nums[i] = rnd.nextInt(10);
			lbl[i].setText(" " + nums[i] + " ");
			lbl[i].setIcon(null);
			for (int j = 0; j < i; j++) {
				if (i != j && nums[i] == nums[j]) {
					count++;
				}
			}
		}

		switch (count) {
			case 0:
				lbl[3].setText("일치하는 숫자 없음");
				break;
			case 1:
				lbl[3].setText("2개의 숫자만 일치");
				break;
			case 3:
				lbl[3].setText("3개의 숫자 모두 일치!!!");
				break;
		}

	}
}
